package model;

import java.util.Collection;

import persistency.controller.CourseController;
import persistency.controller.CourseHistoryController;

import controller.ConfigurationController;

public class RegistrationValidateRequiredCourses extends RegistrationValidator {

	private static final int PASSING_SCORE = Integer.parseInt(ConfigurationController.instance().getProperty("registration.passingScore"));;
	
	public RegistrationValidateRequiredCourses(RegistrationValidationBehavior registrationValidationBehavior){
		super(registrationValidationBehavior);
	}
	
	@Override
	public void validate(Registration registration) throws Exception {
		super.validate(registration);
		Collection<Course> requiredCourses =  CourseController.instance().getByCourseIDRequiredCourses(registration.getOfferedCourse().getCourse().getCode());
		Collection<CourseHistory> courseHistories = CourseHistoryController.instance().getByStudentIDCourseHistories(registration.getStudent().getStudentNumber()); 
		for(Course requiredCourse : requiredCourses){
			boolean coursePassed = false;
			for(CourseHistory ch: courseHistories){
				if(ch.getCourse().equals(requiredCourse) && ch.getScore() >= PASSING_SCORE){
					coursePassed = true;
				}
			}
			if(!coursePassed){
				throw new Exception("Course " + requiredCourse.getName() + " not passed!");
			}
		}
		
	}

}
